package com.learn.smart.framework.helper;

import java.lang.reflect.Field;
import java.util.Map;

import com.learn.smart.framework.annotation.Autowired;
import com.learn.smart.framework.util.ReflectionUtil;

public class IocHelper {
	
	static{
		Map<Class<?>,Object> beanMap = BeanHelper.getBeanMap();
		for(Map.Entry<Class<?>, Object> beanEntry:beanMap.entrySet()){
			Class<?> beanClass = beanEntry.getKey();
			Object beanInsrance = beanEntry.getValue();
			//获取所有属性
			Field[] beanFields = beanClass.getFields();
			if(beanFields!=null&&beanFields.length>0){
				for(Field beanField : beanFields ){
					if(beanField.isAnnotationPresent(Autowired.class)){
						Class<?> beanFieldClass = beanField.getType();
						Object beanFieldInsrance = beanMap.get(beanFieldClass);
						if(beanFieldInsrance!=null){
							ReflectionUtil.setField(beanInsrance, beanField, beanFieldInsrance);
						}
					}
				}
			}
		}
	}
	
	public static void init(){
		System.out.println("依赖注入IOC");
	}

}
